{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <span class=\"exercize\">Exercise: draw the letter H</span>\n",
    "\n",
    "Define a function that takes as input an RGB image and a pair of coordinates (row, column), and returns a copy with a green letter H overlaid at those coordinates. The coordinates point to the top-left corner of the H.\n",
    "\n",
    "The arms and strut of the H should have a width of 3 pixels, and the H itself should have a height of 24 pixels and width of 20 pixels.\n",
    "\n",
    "Start with the following template:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from skimage import img_as_float\n",
    "\n",
    "def draw_H(image, coords, color=(0, 255, 0)):\n",
    "    out = image.copy()\n",
    "    \n",
    "    canvas = out[coords[0]:coords[0] + 24,\n",
    "                 coords[1]:coords[1] + 20]\n",
    "    \n",
    "    canvas[:, :3] = color\n",
    "    canvas[:, -3:] = color\n",
    "    canvas[11:14] = color\n",
    "    \n",
    "    return out"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Test your function like so:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from skimage import data\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "cat = data.chelsea()\n",
    "cat_H = draw_H(cat, (50, -50))\n",
    "plt.imshow(cat_H);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <span class=\"exercize\">Exercise: visualizing RGB channels</span>\n",
    "\n",
    "Display the different color channels of the image along (each as a gray-scale image).  Start with the following template:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# --- read in the image ---\n",
    "\n",
    "image = plt.imread('../../images/Bells-Beach.jpg')\n",
    "\n",
    "# --- assign each color channel to a different variable ---\n",
    "\n",
    "r = image[..., 0]\n",
    "g = image[..., 1]\n",
    "b = image[..., 2]\n",
    "\n",
    "# --- display the image and r, g, b channels ---\n",
    "\n",
    "f, axes = plt.subplots(1, 4, figsize=(16, 5))\n",
    "\n",
    "for ax in axes:\n",
    "    ax.axis('off')\n",
    "\n",
    "(ax_r, ax_g, ax_b, ax_color) = axes\n",
    "    \n",
    "ax_r.imshow(r, cmap='gray')\n",
    "ax_r.set_title('red channel')\n",
    "\n",
    "ax_g.imshow(g, cmap='gray')\n",
    "ax_g.set_title('green channel')\n",
    "\n",
    "ax_b.imshow(b, cmap='gray')\n",
    "ax_b.set_title('blue channel')\n",
    "\n",
    "# --- Here, we stack the R, G, and B layers again\n",
    "#     to form a color image ---\n",
    "ax_color.imshow(np.stack([r, g, b], axis=2))\n",
    "ax_color.set_title('all channels');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, take a look at the following R, G, and B channels.  How would their combination look? (Write some code to confirm your intuition.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from skimage import draw\n",
    "\n",
    "red = np.zeros((300, 300))\n",
    "green = np.zeros((300, 300))\n",
    "blue = np.zeros((300, 300))\n",
    "\n",
    "r, c = draw.disk(center=(100, 100), radius=100)\n",
    "red[r, c] = 1\n",
    "\n",
    "r, c = draw.disk(center=(100, 200), radius=100)\n",
    "green[r, c] = 1\n",
    "\n",
    "r, c = draw.disk(center=(200, 150), radius=100)\n",
    "blue[r, c] = 1\n",
    "\n",
    "f, axes = plt.subplots(1, 3)\n",
    "for (ax, channel) in zip(axes, [red, green, blue]):\n",
    "    ax.imshow(channel, cmap='gray')\n",
    "    ax.axis('off')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.imshow(np.stack([red, green, blue], axis=2));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise: Convert to grayscale (\"black and white\")\n",
    "\n",
    "The *relative luminance* of an image is the intensity of light coming from each point. Different colors contribute differently to the luminance: it's very hard to have a bright, pure blue, for example. So, starting from an RGB image, the luminance is given by:\n",
    "\n",
    "$$\n",
    "Y = 0.2126R + 0.7152G + 0.0722B\n",
    "$$\n",
    "\n",
    "Use Python 3.5's matrix multiplication, `@`, to convert an RGB image to a grayscale luminance image according to the formula above.\n",
    "\n",
    "Compare your results to that obtained with `skimage.color.rgb2gray`.\n",
    "\n",
    "Change the coefficients to 1/3 (i.e., take the mean of the red, green, and blue channels, to see how that approach compares with `rgb2gray`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from skimage import io, color, img_as_float\n",
    "\n",
    "image = img_as_float(io.imread('../../images/balloon.jpg'))\n",
    "\n",
    "gray = color.rgb2gray(image)\n",
    "my_gray = image @ [0.2126, 0.7152, 0.0722]\n",
    "\n",
    "# --- display the results ---\n",
    "\n",
    "f, (ax0, ax1) = plt.subplots(1, 2, figsize=(10, 6))\n",
    "\n",
    "ax0.imshow(gray, cmap='gray')\n",
    "ax0.set_title('skimage.color.rgb2gray')\n",
    "\n",
    "ax1.imshow(my_gray, cmap='gray')\n",
    "ax1.set_title('my rgb2gray');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Let's calculate the mean intensity error, as compared to\n",
    "# scikit-image's color.rgb2gray\n",
    "\n",
    "import numpy as np\n",
    "np.mean(np.abs(gray - my_gray))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
